﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.Diagnostics;

namespace TfsExtensions.Build.Activities
{
    public sealed class InvokeHtmlHelpCommand : CodeActivity
    {
        [RequiredArgument]
        public InArgument<string> HtmlHelpWSFolder { get; set; }

        [RequiredArgument]
        public InArgument<string> SandcastleLocalLanguageCode { get; set; }

        [RequiredArgument]
        public InArgument<string> ChmFolderName { get; set; }

        [RequiredArgument]
        public InArgument<string> AppName { get; set; }

        // If your activity returns a value, derive from CodeActivity<TResult>
        // and return the value from the Execute method.
        protected override void Execute(CodeActivityContext context)
        {
            // no need of IgnoreExitCode argument for this command, it is not defined in hhc.exe syntax.
            //  <Exec Command="&quot;$(HtmlHelpCompilerEXE)&quot; &quot;$(ChmFolderName)\$(AppName).hhp&quot;" IgnoreExitCode="true" />
            var exeFileName = string.Format(@"{0}\hhc.exe", this.HtmlHelpWSFolder.Get(context));

            var chmFolderName = "\"" + string.Format(@"{0}\{1}.hhp", this.ChmFolderName.Get(context), this.AppName.Get(context)) + "\"";

            var arguments = string.Format("{0}", chmFolderName);

            try
            {
                InvokeCommand.ExecuteCommand(context, true, exeFileName, arguments);
            }
            catch (Exception ex)
            {
                LogHelper.LogError(context, ex.Message);
                throw;
            }
        }
    }
}
